import { getUserInfo, getUser } from '@/api/user'
import {
  setStorageToken,
  getStorageToken,
  removeStorageToken,
  setStorageAercle,
  getStorageAercle
} from '@/utils/storage'
export default {
  namespaced: true,
  state: {
    token: getStorageToken(),
    user: {},
    userInfo: {},
    article: getStorageAercle()
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setStorageToken(payload)
    },
    setUserInfo(state, payload) {
      state.user = payload
      state.userInfo = payload.userInfo
    },
    removeToken(state) {
      removeStorageToken()
      state.token = ''
    },
    getArticle(state, payload) {
      state.article = payload
      setStorageAercle(payload)
    }
  },
  actions: {
    async actions_getUserInfo(context, payload) {
      const res = await getUserInfo(payload)
      context.commit('setToken', res.data.token)
    },
    async actions_getUser(context) {
      const res = await getUser()
      context.commit('setUserInfo', res.data)
    }
  }
}
